package com.share.config.aop.encrypt;

import com.alibaba.fastjson.JSON;
import com.share.common.common.Result;
import com.share.common.utils.AESUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Order(5)
@Component
public class EncryptResponseAspectImpl {

    //@Within表示匹配类上的指定注解，@annotation表示匹配方法上的指定注解
    @Pointcut("@within(com.share.config.aop.encrypt.EncryptRespone) || @annotation(com.share.config.aop.encrypt.EncryptRespone)")
    public void joinPointcut() {

    }

    /**
     * 对返回参数加密
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("joinPointcut()")
    public Object round(ProceedingJoinPoint joinPoint) throws Throwable {
        Object obj = joinPoint.proceed();
        if (obj instanceof Result) {
            Result<Object> result = (Result<Object>) obj;
            Object data = result.getData();
            if (data != null) {
                String strData = JSON.toJSONString(data);
                String enStr = AESUtil.encryptByDefaultKey(strData);
                result.setData(enStr);

                //返回自定义的对象
                return result;
            }
        }
        return obj;
    }
}
